package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC781 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int numRabbits(int[] answers) {
            if (null == answers || answers.length <= 0) return 0;

            int totalCount = 0;
            Map<Integer, Integer> map = new HashMap<>();
            for (int answer : answers) {
                if (answer == 0) {
                    ++totalCount;
                    continue;
                }

                if (map.containsKey(answer)) {
                    int count = map.get(answer);
                    if (count > 1) map.put(answer, count - 1);
                    else {
                        map.remove(answer);
                    }
                } else {
                    map.put(answer, answer);
                    totalCount += answer + 1;
                }
            }

            return totalCount;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[0,0,0,1,1]");
        System.out.println(solution.numRabbits(arr));
    }

    public static void main(String[] args) throws Exception {
        LC781 an = new LC781();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
